<template>
    <div class="app-container">
        <div class="filter-container">
            <el-input v-model="listQuery.name" placeholder="名字" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
            <appSelect v-model="listQuery.appId" placeholder="所属应用"></appSelect>
            <companySelect v-model="listQuery.company" placeholder="所属商户"></companySelect>
            <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
                查询
            </el-button>
            <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
                新增
            </el-button>
        </div>

        <el-table
            :key="tableKey"
            v-loading="listLoading"
            :data="list"
            border
            fit
            highlight-current-row
            style="width: 100%;"
            @sort-change="sortChange"
        >

                    <el-table-column label="ID" prop="id" sortable="custom" align="center" width="80">
                <template slot-scope="{row}">
                    <span>{{ row.id }}</span>
                </template>
            </el-table-column>
            <el-table-column label="" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.uId }}</span>
                </template>
            </el-table-column>
            <el-table-column label="" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.couponId }}</span>
                </template>
            </el-table-column>
            <el-table-column label="订单总金额" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.totalAmount }}</span>
                </template>
            </el-table-column>
            <el-table-column label="应付金额（实际支付金额）" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.payAmount }}</span>
                </template>
            </el-table-column>
            <el-table-column label="运费金额" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.freightAmount }}</span>
                </template>
            </el-table-column>
            <el-table-column label="促销优化金额（促销价、满减、阶梯价）" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.promotionAmount }}</span>
                </template>
            </el-table-column>
            <el-table-column label="积分抵扣金额" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.integrationAmount }}</span>
                </template>
            </el-table-column>
            <el-table-column label="优惠券抵扣金额" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.couponAmount }}</span>
                </template>
            </el-table-column>
            <el-table-column label="管理员后台调整订单使用的折扣金额" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.discountAmount }}</span>
                </template>
            </el-table-column>
            <el-table-column label="支付方式：0->未支付；1->支付宝；2->微信" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.payType }}</span>
                </template>
            </el-table-column>
            <el-table-column label="订单来源：0->PC订单；1->app订单" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.sourceType }}</span>
                </template>
            </el-table-column>
            <el-table-column label="订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.status }}</span>
                </template>
            </el-table-column>
            <el-table-column label="订单类型：0->正常订单；1->秒杀订单" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.orderType }}</span>
                </template>
            </el-table-column>
            <el-table-column label="物流公司(配送方式)" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.deliveryCompany }}</span>
                </template>
            </el-table-column>
            <el-table-column label="物流单号" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.deliverySn }}</span>
                </template>
            </el-table-column>
            <el-table-column label="自动确认时间（天）" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.autoConfirmDay }}</span>
                </template>
            </el-table-column>
            <el-table-column label="可以获得的积分" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.integration }}</span>
                </template>
            </el-table-column>
            <el-table-column label="可以活动的成长值" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.growth }}</span>
                </template>
            </el-table-column>
            <el-table-column label="活动信息" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.promotionInfo }}</span>
                </template>
            </el-table-column>
            <el-table-column label="发票类型：0->不开发票；1->电子发票；2->纸质发票" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.billType }}</span>
                </template>
            </el-table-column>
            <el-table-column label="发票抬头" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.billHeader }}</span>
                </template>
            </el-table-column>
            <el-table-column label="发票内容" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.billContent }}</span>
                </template>
            </el-table-column>
            <el-table-column label="收票人电话" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.billReceiverPhone }}</span>
                </template>
            </el-table-column>
            <el-table-column label="收票人邮箱" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.billReceiverEmail }}</span>
                </template>
            </el-table-column>
            <el-table-column label="收货人姓名" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.receiverName }}</span>
                </template>
            </el-table-column>
            <el-table-column label="收货人电话" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.receiverPhone }}</span>
                </template>
            </el-table-column>
            <el-table-column label="收货人邮编" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.receiverPostCode }}</span>
                </template>
            </el-table-column>
            <el-table-column label="省份/直辖市" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.receiverProvince }}</span>
                </template>
            </el-table-column>
            <el-table-column label="城市" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.receiverCity }}</span>
                </template>
            </el-table-column>
            <el-table-column label="区" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.receiverRegion }}</span>
                </template>
            </el-table-column>
            <el-table-column label="详细地址" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.receiverDetailAddress }}</span>
                </template>
            </el-table-column>
            <el-table-column label="订单备注" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.note }}</span>
                </template>
            </el-table-column>
            <el-table-column label="确认收货状态：0->未确认；1->已确认" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.confirmStatus }}</span>
                </template>
            </el-table-column>
            <el-table-column label="删除状态：0->未删除；1->已删除" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.deleteStatus }}</span>
                </template>
            </el-table-column>
            <el-table-column label="下单时使用的积分" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.useIntegration }}</span>
                </template>
            </el-table-column>
            <el-table-column label="支付时间" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.paymentTime }}</span>
                </template>
            </el-table-column>
            <el-table-column label="发货时间" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.deliveryTime }}</span>
                </template>
            </el-table-column>
            <el-table-column label="确认收货时间" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.receiveTime }}</span>
                </template>
            </el-table-column>
            <el-table-column label="评价时间" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.commentTime }}</span>
                </template>
            </el-table-column>
            <el-table-column label="所属商户" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.company }}</span>
                </template>
            </el-table-column>
            <el-table-column label="所属应用" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.appId }}</span>
                </template>
            </el-table-column>
            <el-table-column label="创建时间" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.createTime }}</span>
                </template>
            </el-table-column>
            <el-table-column label="更新时间" width="200px" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.updateTime }}</span>
                </template>
            </el-table-column>

            <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width" fixed="right">
                <template slot-scope="{row,$index}">
                    <el-button type="primary" size="mini" @click="handleUpdate(row)">
                        编辑
                    </el-button>
                    <el-button size="mini" type="danger" @click="handleDelete(row,$index)">
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

        <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
            <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
                <el-form-item label="">
                    <el-input v-model="temp.uId" />
                </el-form-item>
                <el-form-item label="">
                    <el-input v-model="temp.couponId" />
                </el-form-item>
                <el-form-item label="订单总金额">
                    <el-input v-model="temp.totalAmount" />
                </el-form-item>
                <el-form-item label="应付金额（实际支付金额）">
                    <el-input v-model="temp.payAmount" />
                </el-form-item>
                <el-form-item label="运费金额">
                    <el-input v-model="temp.freightAmount" />
                </el-form-item>
                <el-form-item label="促销优化金额（促销价、满减、阶梯价）">
                    <el-input v-model="temp.promotionAmount" />
                </el-form-item>
                <el-form-item label="积分抵扣金额">
                    <el-input v-model="temp.integrationAmount" />
                </el-form-item>
                <el-form-item label="优惠券抵扣金额">
                    <el-input v-model="temp.couponAmount" />
                </el-form-item>
                <el-form-item label="管理员后台调整订单使用的折扣金额">
                    <el-input v-model="temp.discountAmount" />
                </el-form-item>
                <el-form-item label="支付方式：0->未支付；1->支付宝；2->微信">
                    <el-input v-model="temp.payType" />
                </el-form-item>
                <el-form-item label="订单来源：0->PC订单；1->app订单">
                    <el-input v-model="temp.sourceType" />
                </el-form-item>
                <el-form-item label="订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单">
                    <el-input v-model="temp.status" />
                </el-form-item>
                <el-form-item label="订单类型：0->正常订单；1->秒杀订单">
                    <el-input v-model="temp.orderType" />
                </el-form-item>
                <el-form-item label="物流公司(配送方式)">
                    <el-input v-model="temp.deliveryCompany" />
                </el-form-item>
                <el-form-item label="物流单号">
                    <el-input v-model="temp.deliverySn" />
                </el-form-item>
                <el-form-item label="自动确认时间（天）">
                    <el-input v-model="temp.autoConfirmDay" />
                </el-form-item>
                <el-form-item label="可以获得的积分">
                    <el-input v-model="temp.integration" />
                </el-form-item>
                <el-form-item label="可以活动的成长值">
                    <el-input v-model="temp.growth" />
                </el-form-item>
                <el-form-item label="活动信息">
                    <el-input v-model="temp.promotionInfo" />
                </el-form-item>
                <el-form-item label="发票类型：0->不开发票；1->电子发票；2->纸质发票">
                    <el-input v-model="temp.billType" />
                </el-form-item>
                <el-form-item label="发票抬头">
                    <el-input v-model="temp.billHeader" />
                </el-form-item>
                <el-form-item label="发票内容">
                    <el-input v-model="temp.billContent" />
                </el-form-item>
                <el-form-item label="收票人电话">
                    <el-input v-model="temp.billReceiverPhone" />
                </el-form-item>
                <el-form-item label="收票人邮箱">
                    <el-input v-model="temp.billReceiverEmail" />
                </el-form-item>
                <el-form-item label="收货人姓名">
                    <el-input v-model="temp.receiverName" />
                </el-form-item>
                <el-form-item label="收货人电话">
                    <el-input v-model="temp.receiverPhone" />
                </el-form-item>
                <el-form-item label="收货人邮编">
                    <el-input v-model="temp.receiverPostCode" />
                </el-form-item>
                <el-form-item label="省份/直辖市">
                    <el-input v-model="temp.receiverProvince" />
                </el-form-item>
                <el-form-item label="城市">
                    <el-input v-model="temp.receiverCity" />
                </el-form-item>
                <el-form-item label="区">
                    <el-input v-model="temp.receiverRegion" />
                </el-form-item>
                <el-form-item label="详细地址">
                    <el-input v-model="temp.receiverDetailAddress" />
                </el-form-item>
                <el-form-item label="订单备注">
                    <el-input v-model="temp.note" />
                </el-form-item>
                <el-form-item label="确认收货状态：0->未确认；1->已确认">
                    <el-input v-model="temp.confirmStatus" />
                </el-form-item>
                <el-form-item label="删除状态：0->未删除；1->已删除">
                    <el-input v-model="temp.deleteStatus" />
                </el-form-item>
                <el-form-item label="下单时使用的积分">
                    <el-input v-model="temp.useIntegration" />
                </el-form-item>
                <el-form-item label="支付时间">
                    <el-input v-model="temp.paymentTime" />
                </el-form-item>
                <el-form-item label="发货时间">
                    <el-input v-model="temp.deliveryTime" />
                </el-form-item>
                <el-form-item label="确认收货时间">
                    <el-input v-model="temp.receiveTime" />
                </el-form-item>
                <el-form-item label="评价时间">
                    <el-input v-model="temp.commentTime" />
                </el-form-item>
                <el-form-item label="所属商户">
                    <el-input v-model="temp.company" />
                </el-form-item>
                <el-form-item label="所属应用">
                    <el-input v-model="temp.appId" />
                </el-form-item>
                <el-form-item label="创建时间">
                    <el-input v-model="temp.createTime" />
                </el-form-item>
                <el-form-item label="更新时间">
                    <el-input v-model="temp.updateTime" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">
                    取消
                </el-button>
                <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
                    确定
                </el-button>
            </div>
        </el-dialog>

    </div>
</template>

<script>
    import {
        orderList,
        orderCreate,
        orderUpdate,
        orderDelete
    } from '@/api/order'
    import waves from '@/directive/waves'
    import Pagination from '@/components/Pagination'
    import appSelect from '@/views/app/components/appSelect'
    import companySelect from '@/views/app/components/companySelect'

    const defaultListQuery = {
        page: 1,
        limit: 15,
        name: '',
        company: '',
        appId:'',
        sort: '+id'
    };

    const currentTemp = {
        id: undefined,
        uId: undefined,
        couponId: undefined,
        totalAmount: undefined,
        payAmount: undefined,
        freightAmount: undefined,
        promotionAmount: undefined,
        integrationAmount: undefined,
        couponAmount: undefined,
        discountAmount: undefined,
        payType: undefined,
        sourceType: undefined,
        status: undefined,
        orderType: undefined,
        deliveryCompany: undefined,
        deliverySn: undefined,
        autoConfirmDay: undefined,
        integration: undefined,
        growth: undefined,
        promotionInfo: undefined,
        billType: undefined,
        billHeader: undefined,
        billContent: undefined,
        billReceiverPhone: undefined,
        billReceiverEmail: undefined,
        receiverName: undefined,
        receiverPhone: undefined,
        receiverPostCode: undefined,
        receiverProvince: undefined,
        receiverCity: undefined,
        receiverRegion: undefined,
        receiverDetailAddress: undefined,
        note: undefined,
        confirmStatus: undefined,
        deleteStatus: undefined,
        useIntegration: undefined,
        paymentTime: undefined,
        deliveryTime: undefined,
        receiveTime: undefined,
        commentTime: undefined,
        company: undefined,
        appId: undefined,
        createTime: undefined,
        updateTime: undefined,
    }

    export default {
        name: 'order',
        components: { Pagination, appSelect, companySelect },
        directives: { waves },
        filters: {

        },
        data() {
            // var name = (rule, value, callback) => {
            //   if (value === '') {
            //     callback(new Error('模拟后台校验，请输入用户名'));
            //   } else {
            //     callback();
            //   }
            // }
            return {
                tableKey: 0,
                list: null,
                total: 0,
                listLoading: true,
                listQuery: Object.assign({}, defaultListQuery),
                temp: Object.assign({}, currentTemp),
                dialogFormVisible: false,
                dialogStatus: '',
                textMap: {
                    update: '修改',
                    create: '新增'
                },
                rules: {
                    // name: [{ validator: name, trigger: 'blur' }]
                }
            }
        },
        created() {
            this.getList()
        },
        methods: {
            getList() {
                this.listLoading = true
                orderList(this.listQuery).then(response => {
                    this.list = response.data.content
                    this.total = parseInt(response.data.totalElements)

                    this.listLoading = false
                })
            },
            handleFilter() {
                this.listQuery.page = 1
                this.getList()
            },
            sortChange(data) {
                const { prop, order } = data
                if (prop === 'id') {
                    this.sortByID(order)
                }
            },
            sortByID(order) {
                if (order === 'ascending') {
                    this.listQuery.sort = '+id'
                } else {
                    this.listQuery.sort = '-id'
                }
                this.handleFilter()
            },
            resetTemp() {
                this.temp = Object.assign({}, currentTemp)
            },
            handleCreate() {
                this.resetTemp()
                this.dialogStatus = 'create'
                this.dialogFormVisible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].clearValidate()
                })

            },
            createData() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        orderCreate(this.temp).then(() => {
                            this.list.unshift(this.temp)
                            this.dialogFormVisible = false
                            this.$notify({
                                title: '提示',
                                message: '操作成功',
                                type: 'success',
                                duration: 2000
                            })
                            this.getList()
                        })
                    }
                })
            },
            handleUpdate(row) {
                this.temp = Object.assign({}, row) // copy obj
                this.dialogStatus = 'update'
                this.dialogFormVisible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].clearValidate()
                })

            },
            updateData() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        const tempData = Object.assign({}, this.temp)
                        tempData.timestamp = +new Date(tempData.timestamp) // change Thu Nov 30 2017 16:41:05 GMT+0800 (CST) to 1512031311464
                        orderUpdate(tempData).then(() => {
                            const index = this.list.findIndex(v => v.id === this.temp.id)
                            this.list.splice(index, 1, this.temp)
                            this.dialogFormVisible = false
                            this.$notify({
                                title: '提示',
                                message: '操作成功',
                                type: 'success',
                                duration: 2000
                            })
                            this.getList()
                        })
                    }
                })
            },
            handleDelete(row, index) {
                const tempData = {};
                tempData.id = row.id;
                const that = this;
                this.$confirm('确认删除？', '系统提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    cancelButtonClass: 'btn-custom-cancel',
                    type: 'warning'
                }).then(function(){
                    orderDelete(tempData).then(() => {
                        that.$notify({
                            title: '提示',
                            message: '操作成功',
                            type: 'success',
                            duration: 2000
                        })
                        that.getList()
                    })
                }).catch(function(){
                    // console.log("取消成功！");
                })
            }
        }
    }
</script>

<style lang="scss" scoped>


</style>
